package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.whmnx.base.bean.PersonalBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.router.RouterFragmentPath;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.my.databinding.FragmentMeBinding;
import com.whmnx.my.ui.activity.MyLocationActivity;
import com.whmnx.my.ui.base.ViewPagerGroupFragment;
import com.whmnx.my.ui.fragment.MyBalanceFragment;
import com.whmnx.my.ui.fragment.MyEditInfoFragment;
import com.whmnx.my.ui.fragment.MyFragment;
import com.whmnx.my.ui.fragment.MyGeneralFragment;
import com.whmnx.my.ui.fragment.MyGiveRewardFragment;
import com.whmnx.my.ui.fragment.MyKefuFragment;
import com.whmnx.my.ui.fragment.MyOrderFragment;
import com.whmnx.my.ui.fragment.MyRenZhengFragment;
import com.whmnx.my.ui.fragment.MySettingFragment;
import com.whmnx.my.ui.fragment.MyTeamFragment;
import com.whmnx.my.ui.fragment.MyTeamWeixinFragment;
import com.whmnx.my.ui.fragment.MyWithdrawFragment;
import com.whmnx.my.ui.fragment.MyYaoQingFragment;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 *
 * @author goldze
 * @date 2018/6/21
 */

public class MeViewModel extends ToolbarViewModel {
    MyFragment mFragment;

    public ObservableField<String> Code = new ObservableField<>("");
    public ObservableField<String> focusNum = new ObservableField<>("");
    public ObservableField<String> UserInfo_RealName = new ObservableField<>("");
    public ObservableField<String> UserInfo_Lv = new ObservableField<>("");
    public ObservableField<String> UserInfo_Currency = new ObservableField<>("");
    public ObservableField<String> TotalIncome = new ObservableField<>("");
    public ObservableField<String> UserInfo_GetMoney = new ObservableField<>("");
    public ObservableField<String> UserInfo_Bean = new ObservableField<>("");

    private Disposable subscribe;
    FragmentMeBinding mBinding;
    private double aDouble;


    public MeViewModel(@NonNull Application application) {
        super(application);
    }

    //财务管理
    public BindingCommand caiwuOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyGeneralFragment.class.getCanonicalName());
            //  ARouter.getInstance().build(RouterFragmentPath.User.MY_GUANLI).navigation();
        }
    });
    //我的订单
    public BindingCommand dingdanOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyOrderFragment.class.getCanonicalName());
           // startContainerActivity(ViewPagerGroupFragment2.class.getCanonicalName());
        }
    });
    //我的认证
    public BindingCommand renzhengOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(MyRenZhengFragment.class);
        }
    });
    //我的地址
    public BindingCommand locOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
          //  startContainerActivity(MyLoctionFragment.class.getCanonicalName());
             startActivity(MyLocationActivity.class);
            //  ARouter.getInstance().build(RouterFragmentPath.User.MY_GUANLI).navigation();
        }
    });
    //播豆
    public BindingCommand bodouOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(ViewPagerGroupFragment.class.getCanonicalName());
        }
    });
    //更换图片
    public BindingCommand imgOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(MyEditInfoFragment.class);
        }
    });
    //我的团队
    public BindingCommand teamOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyTeamFragment.class.getCanonicalName());
        }
    });
    //客服
    public BindingCommand kefuOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyKefuFragment.class.getCanonicalName());
        }
    });

    //邀请好友
    public BindingCommand yaoOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyYaoQingFragment.class.getCanonicalName());
        }
    });
    //总收入
    public BindingCommand OnClickCommand1 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyGeneralFragment.class.getCanonicalName());
        }
    });
    //可提现金额
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyWithdrawFragment.class.getCanonicalName());
        }
    });
    //资产余额
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyBalanceFragment.class.getCanonicalName());
        }
    });
    //打赏金额
    public BindingCommand OnClickCommand4 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyGiveRewardFragment.class.getCanonicalName());
        }
    });
    //团队威信群
    public BindingCommand temaOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(MyTeamWeixinFragment.class.getCanonicalName());
        }
    });


    /**
     * 右边图标的点击事件，子类可重写
     */
    @Override
    protected void rightIconOnClick() {
        ARouter.getInstance().build(RouterFragmentPath.Main.PAGER_MAIN).navigation();
    }


    public void initToolbar(MyFragment myFragment, FragmentMeBinding binding) {
        this.mFragment = myFragment;
        this.mBinding = binding;
        setTitleText("个人中心");
        setRightIconVisible(View.VISIBLE);
        setRightIconSetVisible(View.VISIBLE);
        setLeftIconSetVisible(View.GONE);
        getUserInfo();
        initListener();
    }

    public void initView() {
        int pro =  SPUtils.getInstance().getInt("pro");
        mBinding.progressBarH.setProgress(pro);
        float a = (pro/300)*100;
        DecimalFormat df = new DecimalFormat("0.00");
        String b = df.format((float)pro/300);
        aDouble = (Double.parseDouble(b) * 100);
        String c = df.format(aDouble);
        mBinding.tv2.setText(c + "%");
    }


    private void initListener() {
        subscribe = RxBus.getDefault().toObservable(Object.class)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object){
                        if (object.equals(EventConstants.IMG)){
                            getUserInfo();
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    public void getUserInfo() {
        Map<String, Object> params = new HashMap<>();
        params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.get(mFragment.getActivity(), Constants.PERSONALINFORMATION, params,new CallBack<PersonalBean>() {
            @Override
            public void onSuccess(PersonalBean personalBean) {
                ImageUtil.setImage(mBinding.rvUser,personalBean.getUserInfo_HeadImg());
                UserInfo_RealName.set(personalBean.getUserInfo_NickName());
                Code.set("ID: " + personalBean.Code);
                focusNum.set(personalBean.getFocusNum() +  "关注" + " | " + personalBean.getFansNum() +  "粉丝" );
                DecimalFormat df = new DecimalFormat("0.00");
                UserInfo_Currency.set(df.format(personalBean.getUserInfo_Currency()));
                UserInfo_GetMoney.set(df.format(personalBean.getUserInfo_GetMoney()));
                UserInfo_Bean.set(df.format(personalBean.getUserInfo_Bean()));
                TotalIncome.set(df.format(personalBean.TotalIncome));
                SPUtils.getInstance().put(Constants.LV, personalBean.getUserInfo_Lv());

                SPUtils.getInstance().put(Constants.PAYWORD, personalBean.getUserInfo_SafePwd());
                SPUtils.getInstance().put(Constants.NICKNAME, personalBean.getUserInfo_NickName());
                SPUtils.getInstance().put(Constants.CODE, personalBean.Code);
                if (personalBean.getUserInfo_Lv() == 0){
                    UserInfo_Lv.set("未认证");
                }else if (personalBean.getUserInfo_Lv() == 1){
                    UserInfo_Lv.set("V1认证");
                }else {
                    UserInfo_Lv.set("V2认证");
                }

                initView();
            }
        });
    }


    @Override
    public void rightIconTwoOnClick() {
        startContainerActivity(MySettingFragment.class.getCanonicalName());
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
